/**
 * FileName: ShareController
 * Author:   Leopold
 * Date:     2018/10/1 18:08
 * Description:
 * History:
 */
package cn.mcsys.worker.core.web;

import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirCar;
import cn.mcsys.myadmin.modules.air.entity.AirCarRecord;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.share.dto.AirCarDTO;
import cn.mcsys.myadmin.modules.share.dto.BookingPeopleDTO;
import cn.mcsys.myadmin.modules.viewer.entity.AirTickets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class ShareController {

    @Autowired
    private BackService backService;

    @RequestMapping("bookingCount")
    @ResponseBody
    public Map<String, BookingPeopleDTO> dtos() {
        List<AirCar> cars = backService.getAllCarList();
        Map<String, BookingPeopleDTO> map = new HashMap<>();
//        查询各个乘车点对应的在途数，总预约数，已达到，未送达，
        Map<String,List<AirCar>> pointCars = new HashMap<>();
//        将各个乘车点的车辆进行归类
        for (AirCar car : cars) {
            String pointName = car.getAirRidePoint().getName();
            List<AirCar> airCars = pointCars.get(pointName);
            if(airCars == null){
                List<AirCar> carList = new ArrayList<>();
                carList.add(car);
                pointCars.put(pointName,carList);
            }else{
                airCars.add(car);
            }
        }
        Set<Map.Entry<String, List<AirCar>>> entries = pointCars.entrySet();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map.Entry<String, List<AirCar>> entry : entries) {
            BookingPeopleDTO bookingPeopleDTO = new BookingPeopleDTO();
            map.put(entry.getKey(),bookingPeopleDTO);
            List<AirCar> carsList = entry.getValue();
            AirRidePoint airRidePoint = null;
            for (AirCar airCar : carsList) {
                airRidePoint = airCar.getAirRidePoint();
                if (airCar.getStatus() != null){
                    if(airCar.getStatus() == AirCar.RIDING){
//                    查询该车辆最新的发车记录(当天)
                        AirCarRecord airCarRecord = backService.selectLatestCarRecord(airCar,AirCar.RIDING);
                        if(airCarRecord != null){
                            int passengerSum = bookingPeopleDTO.getRidingSum()+ airCarRecord.getPassengerSum();
                            bookingPeopleDTO.setRidingSum(passengerSum);
                        }
                    }
                    if(airCar.getStatus() == AirCar.ARRIVE){
//                    查询该车辆最新的发车记录
                        AirCarRecord airCarRecord = backService.selectLatestCarRecord(airCar,AirCar.ARRIVE);
                        if(airCarRecord != null){
                            int passengerSum = bookingPeopleDTO.getArriveSum()+ airCarRecord.getPassengerSum();
                            bookingPeopleDTO.setArriveSum(passengerSum);
                        }
                    }
                }

            }
//            当天总预约人数
            int bookingSum = backService.selectBookingSumByPointId(airRidePoint.getId());
            int notArriveSum = bookingSum - bookingPeopleDTO.getArriveSum() - bookingPeopleDTO.getRidingSum();
            bookingPeopleDTO.setNotArriveSum(notArriveSum);
            bookingPeopleDTO.setBookingSum(bookingSum);
            bookingPeopleDTO.setDate(format.format(new Date()));
        }

        return map;
    }

    /**
     * 获取票务数据
     * @param beginDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    @RequestMapping("getTickets")
    @ResponseBody
    public List<AirTickets> getTickets(String beginDate,String endDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begin = format.parse(beginDate);
        Date end = format.parse(endDate);
        List<AirTickets> tickets = backService.selectTickets(begin,end);
        return tickets;
    }

    @RequestMapping("getCars")
    @ResponseBody
    public List<AirCarDTO> getCars(){
        return backService.trafficList();
    }



}
